package bean;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Enumeration;

import javax.imageio.ImageIO;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class MemberDTO {

	private MemberVO vo;
	private Connection conn;
	public MemberDTO() {
		conn = new MyOracle().getConn();
	}
	
	public void setVo(MemberVO vo){
		this.vo = vo;
	}
	public boolean login(){
		boolean bolean = false;
		
		try{
			
			String sql = "select m_id, m_pw from member_ where m_id=? and m_pw=?";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getM_id());
			pst.setString(2, vo.getM_pw());
			
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				bolean = true;
			}
			pst.close();
			conn.close();
		}catch(Exception e){
			
		}
		return bolean;
	}
	
	public boolean signup(){

		try {
			String sql = "insert into member_ "
					+ " values(?,?,?,?,?,?,?,?) ";
			
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getM_id());
			pst.setString(2, vo.getM_pw());
			pst.setString(3, vo.getM_gender());
			pst.setString(4, vo.getM_local());
			pst.setString(5, vo.getM_job());
			pst.setInt(6, vo.getM_age());
			pst.setString(7, vo.getM_photo());
			pst.setString(8, vo.getM_ka());
			
			int a= pst.executeUpdate();
			
			pst.close();
			conn.close();
			
			if(a >0){
				return true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}
	
	public void fileUpLoad(MemberVO vo) {
		this.vo = vo;

		int size = 1024 * 1024 * 10;
		String saveDir = "D:/jhta/workspace_java/semy2/WebContent/src/member/memberPhoto";
		String oriFile = "";
		String sysFile = "";

		MultipartRequest multi = null;
		try {
			multi = new MultipartRequest(vo.getRequest(), saveDir, size,
					"utf-8", new DefaultFileRenamePolicy());

			Enumeration files = multi.getFileNames();
			String tempFile = (String) files.nextElement();
			oriFile = multi.getOriginalFileName(tempFile);
			sysFile = multi.getFilesystemName(tempFile);

			vo.setM_id(multi.getParameter("m_id"));
			vo.setM_pw(multi.getParameter("m_pw"));
			vo.setM_gender(multi.getParameter("m_gender"));
			vo.setM_local(multi.getParameter("m_local"));
			vo.setM_job(multi.getParameter("m_job"));
			vo.setM_age(Integer.parseInt(multi.getParameter("m_age")));
			vo.setM_photo(sysFile);
			vo.setM_ka(multi.getParameter("m_ka"));
			

			// 섬 네일 파일 추가 생성
			 ParameterBlock pd = new ParameterBlock();
			 pd.add(saveDir + sysFile);
			 RenderedOp rop = JAI.create("fileload", pd);
			 
			 BufferedImage bi = rop.getAsBufferedImage();
			 BufferedImage thumb = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
			 
			 Graphics2D g = thumb.createGraphics();
			 g.drawImage(bi, 0, 0, 100, 100, null);
			 
			 File file = new File(saveDir + "/sm_" + sysFile);
			 ImageIO.write(thumb, "jpg", file);
			 
			// db에 저장
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public MemberVO selectOne(String m_id){			// view를 위한 처리 
		MemberVO vo = new MemberVO();
		String sql = "select * from member_ where m_id = ?";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, m_id);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				vo.setM_id(rs.getString("m_id"));
				vo.setM_gender(rs.getString("m_gender"));
				vo.setM_local(rs.getString("m_local"));
				vo.setM_job(rs.getString("m_job"));
				vo.setM_age(rs.getInt("m_age"));
				vo.setM_photo(rs.getString("m_photo"));
				vo.setM_ka(rs.getString("m_ka"));
			}
			rs.close();
			pst.close();
			conn.close();
		} catch (Exception e) {

		}
		return vo;
	}
	
	public MemberVO getVo() {
		return vo;
	}
}
